Page({

  /**
   * 页面的初始数据
   */
  data: {
    eIndex: 0,
    products: [],
    pageSize: 10,
    pageNumber: 1 
  },

  // 菜单切换类名on
  tab: function (e) {
    var eIndex = e.currentTarget.dataset.index
    this.setData({
      eIndex: eIndex
    })
    var carType=''
    if (eIndex==0){
      carType=''
    } else if (eIndex == 1){
      carType = '轿车'
    } else if (eIndex == 2) {
      carType = '撩妹'
    } else if (eIndex == 3) {
      carType = '商务'
    }
    this.setData({
      carType: carType,
      pageNumber: 1,
      products:[],
      categoryId:'',
      startPrice:'',
      lastPrice:''
    })
    this.loadProduct()

  },

  // 跳转到筛选
  toFilter: function () {
    wx.redirectTo({
      url: '../filter/filter'
    })
  },

  // 跳转到详情
  toDetails: function (e) {
    var productId = e.currentTarget.dataset.value
    wx.navigateTo({
      url: '../details/details?productId=' + productId
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var carType=options.carType
    var categoryId = options.categoryId
    var startPrice = options.startPrice
    var lastPrice = options.lastPrice
    this.setData({
      carType: carType ? carType:'',
      categoryId: categoryId ? categoryId : '',
      startPrice: startPrice ? startPrice : '',
      lastPrice: lastPrice ? lastPrice : ''
    })
    this.loadProduct()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let pageNumber = this.data.pageNumber

    pageNumber = pageNumber + 1
    this.setData({
      pageNumber: pageNumber
    });
    this.loadProduct()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '琼崖租车',
      path: '/pages/index/index'
    }
  },
  loadProduct: function () {
    let that = this
    //加载推荐商品
    wx.request({
      url: 'https://small.qiongyazuche.com/sunday/small/product/select',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        'pageSize': that.data.pageSize,
        'pageNumber': that.data.pageNumber,
        'carType': that.data.carType,
        'categoryId': that.data.categoryId,
        'startPrice': that.data.startPrice,
        'lastPrice': that.data.lastPrice,
        'sort':'sequence',
        'order':'asc'

      },
      success: function (res) {

        let products = that.data.products.concat(res.data.result)
        that.setData({
          products: products
        })
      }
    })

  }
  
})